<title>直接请求</title>
<meta charset="UTF-8"/>
<iframe src="index.html"  style="position: absolute;right:0;top:1%;min-height: 300px;border-width: 0px">
</iframe>
<h1>1.初入门径--直接请求</h1>
<h2>
<pre>
    简单的http请求，直接解析json和html
    推荐html库
    go  goquery
    php  symfony/dom-crawler
    <a href="../index.html" target="">小说下载工具</a>
</pre>
</h2>
<h2>php发送GET和POST</h2>
<pre  style="background:#f0f0f0;white-space: pre;text-align: left" >
 public  static function get($url,$header=[],$ip=0,$port=0,$proxy_type="http"){
        if ($header) {
            $header = array_merge(array(
                //"Accept-Language: en-US",
                //"User-Agent: UPS/15 CFNetwork/1240.0.2 Darwin/20.6.3",
                'Accept-Encoding: gzip, deflate',
            ), $header);
        }
        // echo $url;
        $curl = curl_init();
        $content =[];
        $is_arr = false;
        if (!is_array($url)){
            $url=[$url];
        }else{
            $is_arr = true;
        }
        foreach ( $url  as $u) {
            curl_setopt($curl, CURLOPT_URL, $u);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//结果不输出到页面
            if (is_array($header)&&$header){
                curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            }
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            curl_setopt($curl, CURLOPT_ENCODING, 'gzip, deflate, br');
            //curl_setopt($curl,CURLOPT_VERBOSE,true);
            if (!empty($port) && !empty($ip)) {
                //echo $ip.':'.$port.PHP_EOL;
                curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
                curl_setopt($curl, CURLOPT_PROXY, $ip); //代理服务器地址
                curl_setopt($curl, CURLOPT_PROXYPORT, $port); //代理服务器端口
                if ($proxy_type=="http"){
                    curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
                }
                if ($proxy_type=="https"){
                    curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
                }

            }
            $content[] = curl_exec($curl);

        }

        curl_close($curl);
        unset($curl);
        if ($is_arr){
            return $content;
        }else{
            return $content[0];
        }

    }

    public  static function post($url,$data=[],$header=[],$ip=0,$port=0,$proxy_type="http"){
        if ($header) {
            $header[]=   'Accept-Encoding: gzip, deflate';
        }
        // echo $url;
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//结果不输出到页面
        if (is_array($header)&&$header) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_TIMEOUT,300);
        curl_setopt($curl, CURLOPT_ENCODING, '');
        //curl_setopt($curl,CURLOPT_VERBOSE,true);

        //curl_setopt($curl, CURLINFO_HEADER_OUT, true);
        // curl_setopt($curl , CURLOPT_HEADER, true );
        if(!empty($port) && !empty($ip)){
            //echo $ip.':'.$port.PHP_EOL;
            curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
            curl_setopt($curl, CURLOPT_PROXY, $ip); //代理服务器地址
            curl_setopt($curl, CURLOPT_PROXYPORT, $port); //代理服务器端口
            if ($proxy_type=="http"){
                curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
            }
            if ($proxy_type=="https"){
                curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
            }
        }

        $content = curl_exec($curl);
        curl_close($curl);
        unset($curl);



        return $content;
    }

</pre>